package zengjin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import zengjin.common.BeanCopyUtil;
import zengjin.common.PageResponse;
import zengjin.exception.CustomException;
import zengjin.mapper.EmployerMapper;
import zengjin.mapper.ImageMapper;
import zengjin.mapper.JobMapper;
import zengjin.model.dto.job.AddJobRequest;
import zengjin.model.dto.job.PageJobRequest;
import zengjin.model.dto.job.UpdateJobRequest;
import zengjin.model.en.Employer;
import zengjin.model.en.Image;
import zengjin.model.en.Job;
import zengjin.model.vo.job.JobAndEmployerVO;
import zengjin.model.vo.job.JobVO;
import zengjin.service.JobService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 19324
 * @description 针对表【job】的数据库操作Service实现
 * @createDate 2025-01-02 14:22:19
 */
@Service
public class JobServiceImpl extends ServiceImpl<JobMapper, Job>
        implements JobService {

    @Resource
    private JobMapper jobMapper;

    @Resource
    private EmployerMapper employerMapper;

    @Resource
    private ImageMapper imageMapper;

    @Override
    public Boolean addJob(AddJobRequest addJobRequest) {
        //1.获取参数
        String jobStatus = addJobRequest.getJobStatus();
        Long jobEmployerId = addJobRequest.getJobEmployerId();
        String jobTitle = addJobRequest.getJobTitle();
        String jobDescription = addJobRequest.getJobDescription();
        String jobRequirements = addJobRequest.getJobRequirements();
        String jobLocation = addJobRequest.getJobLocation();
        String jobType = addJobRequest.getJobType();
        Integer jobSalary = addJobRequest.getJobSalary();
        String jobSalaryType = addJobRequest.getJobSalaryType();
        String jobWorkingHours = addJobRequest.getJobWorkingHours();
        Date jobGiveStartDate = addJobRequest.getJobGiveStartDate();
        Date jobGiveEndDate = addJobRequest.getJobGiveEndDate();
        Date jobStartDate = addJobRequest.getJobStartDate();
        Date jobEndDate = addJobRequest.getJobEndDate();

        //2.创建Job对象
        Job job = new Job();
        job.setJobAdminStatus("正常");
        job.setJobStatus(jobStatus);
        job.setJobEmployerId(jobEmployerId);
        job.setJobTitle(jobTitle);
        job.setJobDescription(jobDescription);
        job.setJobRequirements(jobRequirements);
        job.setJobLocation(jobLocation);
        job.setJobType(jobType);
        job.setJobSalary(jobSalary);
        job.setJobSalaryType(jobSalaryType);
        job.setJobWorkingHours(jobWorkingHours);
        job.setJobGiveStartDate(jobGiveStartDate);
        job.setJobGiveEndDate(jobGiveEndDate);
        job.setJobStartDate(jobStartDate);
        job.setJobEndDate(jobEndDate);

        //3.调用方法
        return jobMapper.insert(job) > 0;
    }

    @Override
    public PageResponse<JobVO> pageJob(PageJobRequest pageJobRequest) {
        //1.获取参数
        String searchName = pageJobRequest.getSearchName();
        String jobType = pageJobRequest.getJobType();
        String jobSalaryType = pageJobRequest.getJobSalaryType();
        String jobLocation = pageJobRequest.getJobLocation();
        String jobStatus = pageJobRequest.getJobStatus();
        String jobAdminStatus = pageJobRequest.getJobAdminStatus();
        Long jobEmployerId = pageJobRequest.getJobEmployerId();

        //2.构建分页条件
        Page<Job> page = pageJobRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Job> jobPage = lambdaQuery()
                .eq(jobEmployerId != null, Job::getJobEmployerId, jobEmployerId)
                .eq(StrUtil.isNotBlank(jobType), Job::getJobType, jobType)
                .eq(StrUtil.isNotBlank(jobSalaryType), Job::getJobSalaryType, jobSalaryType)
                .eq(StrUtil.isNotBlank(jobLocation), Job::getJobLocation, jobLocation)
                .eq(StrUtil.isNotBlank(jobStatus), Job::getJobStatus, jobStatus)
                .eq(StrUtil.isNotBlank(jobAdminStatus), Job::getJobAdminStatus, jobAdminStatus)
                .and(StrUtil.isNotBlank(searchName),
                        w -> w.like(Job::getJobTitle, searchName)
                                .or()
                                .like(Job::getJobSalary, searchName))
                .page(page);

        return PageResponse.of(jobPage, JobVO.class);
    }

    @Override
    public PageResponse<JobAndEmployerVO> pageJobAndEmployer(PageJobRequest pageJobRequest) {
        //1.获取参数
        String searchName = pageJobRequest.getSearchName();
        String jobType = pageJobRequest.getJobType();
        String jobSalaryType = pageJobRequest.getJobSalaryType();
        String jobLocation = pageJobRequest.getJobLocation();
        String jobStatus = pageJobRequest.getJobStatus();
        String jobAdminStatus = pageJobRequest.getJobAdminStatus();

        //2.构建查询条件
        QueryWrapper<Job> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(jobType), "job_type", jobType)
                .eq(StrUtil.isNotBlank(jobSalaryType), "job_salary_type", jobSalaryType)
                .like(StrUtil.isNotBlank(jobLocation), "job_location", jobLocation)
                .eq(StrUtil.isNotBlank(jobStatus), "job_status", jobStatus)
                .eq(StrUtil.isNotBlank(jobAdminStatus), "job_admin_status", jobAdminStatus)
                .and(StrUtil.isNotBlank(searchName),
                        w -> w.like("job_title", searchName)
                                .or()
                                .like("job_salary", searchName))
                .orderByDesc("update_time");

        //3.查询出结果
        List<Job> jobs = jobMapper.selectList(queryWrapper);
        List<JobAndEmployerVO> jobAndEmployerVOS = new ArrayList<>();
        jobs.forEach(job -> {
            if (job.getJobEmployerId() != null) {
                Employer employer = employerMapper.selectById(job.getJobEmployerId());
                JobAndEmployerVO jobAndEmployerVO = BeanCopyUtil.copyPropertiesToVo(job, employer, JobAndEmployerVO.class);
                //查询头像
                QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
                imageQueryWrapper.eq("image_name", "employer_avatar");
                imageQueryWrapper.eq("image_name_id", employer.getEmployerId());
                Image image = imageMapper.selectOne(imageQueryWrapper);
                if (image != null) {
                    jobAndEmployerVO.setUserAvatarUrl(image.getImageUrl());
                }
                jobAndEmployerVOS.add(jobAndEmployerVO);
            }
        });

        //4.构建分页条件
        Page<Job> page = pageJobRequest.toPageDefaultSortByUpdateTime();

        return PageResponse.of(page, jobAndEmployerVOS);

    }

    @Override
    public Boolean updateJobById(UpdateJobRequest updateJobRequest) {
        //1.获取参数
        Long jobId = updateJobRequest.getJobId();
        String jobStatus = updateJobRequest.getJobStatus();
        String jobTitle = updateJobRequest.getJobTitle();
        String jobDescription = updateJobRequest.getJobDescription();
        String jobRequirements = updateJobRequest.getJobRequirements();
        String jobLocation = updateJobRequest.getJobLocation();
        String jobType = updateJobRequest.getJobType();
        Integer jobSalary = updateJobRequest.getJobSalary();
        String jobSalaryType = updateJobRequest.getJobSalaryType();
        String jobWorkingHours = updateJobRequest.getJobWorkingHours();
        Date jobGiveStartDate = updateJobRequest.getJobGiveStartDate();
        Date jobGiveEndDate = updateJobRequest.getJobGiveEndDate();
        Date jobStartDate = updateJobRequest.getJobStartDate();
        Date jobEndDate = updateJobRequest.getJobEndDate();

        //2.更新岗位信息
        LambdaUpdateWrapper<Job> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Job::getJobId, jobId)
                .set(StrUtil.isNotBlank(jobStatus), Job::getJobStatus, jobStatus)
                .set(StrUtil.isNotBlank(jobTitle), Job::getJobTitle, jobTitle)
                .set(StrUtil.isNotBlank(jobDescription), Job::getJobDescription, jobDescription)
                .set(StrUtil.isNotBlank(jobRequirements), Job::getJobRequirements, jobRequirements)
                .set(StrUtil.isNotBlank(jobLocation), Job::getJobLocation, jobLocation)
                .set(StrUtil.isNotBlank(jobType), Job::getJobType, jobType)
                .set(jobSalary != null, Job::getJobSalary, jobSalary)
                .set(StrUtil.isNotBlank(jobSalaryType), Job::getJobSalaryType, jobSalaryType)
                .set(StrUtil.isNotBlank(jobWorkingHours), Job::getJobWorkingHours, jobWorkingHours)
                .set(jobGiveStartDate != null, Job::getJobGiveStartDate, jobGiveStartDate)
                .set(jobGiveEndDate != null, Job::getJobGiveEndDate, jobGiveEndDate)
                .set(jobStartDate != null, Job::getJobStartDate, jobStartDate)
                .set(jobEndDate != null, Job::getJobEndDate, jobEndDate);
        return jobMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public JobAndEmployerVO getJobAndEmployerDetailByJobId(Long jobId) {
        //1.根据jobId查询岗位信息
        Job job = jobMapper.selectById(jobId);
        if (job == null) {
            throw new RuntimeException("岗位不存在");
        }
        //判断是否过了招聘周期
        if (job.getJobGiveEndDate().before(new Date())) {
            job.setJobStatus("停止招聘");
            jobMapper.updateById(job);
            throw new CustomException("岗位已结束招聘");
        }


        //2.根据employerId查询 employer信息
        Employer employer = employerMapper.selectById(job.getJobEmployerId());
        //3.根据转化为VO
        JobAndEmployerVO jobAndEmployerVO = BeanCopyUtil.copyPropertiesToVo(job, employer, JobAndEmployerVO.class);
        if (employer != null) {
            //查询头像
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("image_name", "employer_avatar");
            imageQueryWrapper.eq("image_name_id", employer.getEmployerId());
            Image image = imageMapper.selectOne(imageQueryWrapper);
            if (image != null) {
                jobAndEmployerVO.setUserAvatarUrl(image.getImageUrl());
            }
        }
        return jobAndEmployerVO;
    }

    @Override
    public PageResponse<JobVO> getJobAdminPage(PageJobRequest pageJobRequest) {
        //1.获取参数
        String searchName = pageJobRequest.getSearchName();
        String jobType = pageJobRequest.getJobType();
        String jobSalaryType = pageJobRequest.getJobSalaryType();
        String jobLocation = pageJobRequest.getJobLocation();

        //2.构建分页条件
        Page<Job> page = pageJobRequest.toPageDefaultSortByUpdateTime();

        //3.分页查询
        Page<Job> jobPage = lambdaQuery()
                .eq(StrUtil.isNotBlank(jobType), Job::getJobType, jobType)
                .eq(StrUtil.isNotBlank(jobSalaryType), Job::getJobSalaryType, jobSalaryType)
                .like(StrUtil.isNotBlank(jobLocation), Job::getJobLocation, jobLocation)
                .and(StrUtil.isNotBlank(searchName),
                        w -> w.like(Job::getJobTitle, searchName)
                                .or()
                                .like(Job::getJobSalary, searchName))
                .page(page);

        return PageResponse.of(jobPage, JobVO.class);
    }


}




